package com.pace.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author du.senlin
 * @Title:
 * @Description:
 * @date 2020/3/30 16:13
 */
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    /**
     * @param name
     * @return
     * @HystrixCommand注解。该注解对该方法创建了熔断器的功能， 并指定了fallbackMethod熔断方法，熔断方法直接返回了一个字符串，
     * 字符串为”hi,”+name+”,sorry,error!”，代码如下：
     */
    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        // PACE-RIBBON为应用名称
        return restTemplate.getForObject("http://PACE-RIBBON/hi?name=" + name, String.class);
    }

    public String hiError(String name) {
        return "hi," + name + ",sorry,error!";
    }
}
